#include "stdafx.h"
#include "CD3D9VertexDeclaration.h"
#include "mywow.h"

#include "compileconfig.h"

#ifdef MW_COMPILE_WITH_DIRECT3D9

#include "CD3D9Driver.h"

//vertex declaration

D3DVERTEXELEMENT9 CD3D9VertexDeclaration::Decl_P[] = 
{
	{0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					//position

	D3DDECL_END()
};

D3DVERTEXELEMENT9 CD3D9VertexDeclaration::Decl_PC[] = 
{
	{0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					//position
	{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},					//color

	D3DDECL_END()
};

D3DVERTEXELEMENT9 CD3D9VertexDeclaration::Decl_PCT[] =
{
	{0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					//position
	{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
	{0, 16, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},				//tex

	D3DDECL_END()
};

D3DVERTEXELEMENT9 CD3D9VertexDeclaration::Decl_PN[] = 
{
	{0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					//position
	{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},					//normal
	D3DDECL_END()
};

D3DVERTEXELEMENT9 CD3D9VertexDeclaration::Decl_PNC[] = 
{
	{0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					//position
	{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},					//normal
	{0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},      //color
	D3DDECL_END()
};

D3DVERTEXELEMENT9 CD3D9VertexDeclaration::Decl_PNT[] = 
{
	{0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					//position
	{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},					//normal
	{0, 24, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},					//texture
	D3DDECL_END()
};

D3DVERTEXELEMENT9 CD3D9VertexDeclaration::Decl_PT[] = 
{
	{0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					//position
	{0, 12, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},					//texture
	D3DDECL_END()
};

D3DVERTEXELEMENT9 CD3D9VertexDeclaration::Decl_PNCT[] = 
{
	{0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					//position
	{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},					//normal
	{0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},					//color
	{0, 28, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
	D3DDECL_END()
};

D3DVERTEXELEMENT9 CD3D9VertexDeclaration::Decl_PNCT2[] = 
{
	{0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					//position
	{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},					//normal
	{0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},					//color
	{0, 28, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
	{0, 36, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
	D3DDECL_END()
};

D3DVERTEXELEMENT9 CD3D9VertexDeclaration::Decl_PNT2W_M[] =
{
	{0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					//position
	{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
	{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
	{0, 32, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
	{0, 40, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT,0},
	{1, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES,0},
	D3DDECL_END()
};

CD3D9VertexDeclaration::CD3D9VertexDeclaration( E_VERTEX_TYPE vtype )
	: VertexType(vtype)
{
	IDirect3DDevice9* pID3DDevice = static_cast<CD3D9Driver*>(g_Engine->getDriver())->pID3DDevice;

	switch (VertexType)
	{
	case EVT_P:
		pID3DDevice->CreateVertexDeclaration(Decl_P, &Declaration);
		break;
	case EVT_PC:
		pID3DDevice->CreateVertexDeclaration(Decl_PC, &Declaration);
		break;
	case EVT_PCT:
		pID3DDevice->CreateVertexDeclaration(Decl_PCT, &Declaration);
		break;
	case EVT_PN:
		pID3DDevice->CreateVertexDeclaration(Decl_PN, &Declaration);
		break;
	case EVT_PNC:
		pID3DDevice->CreateVertexDeclaration(Decl_PNC, &Declaration);
		break;
	case EVT_PNT:
		pID3DDevice->CreateVertexDeclaration(Decl_PNT, &Declaration);
		break;
	case EVT_PT:
		pID3DDevice->CreateVertexDeclaration(Decl_PT, &Declaration);
		break;
	case EVT_PNCT:
		pID3DDevice->CreateVertexDeclaration(Decl_PNCT, &Declaration);
		break;
	case EVT_PNCT2:
		pID3DDevice->CreateVertexDeclaration(Decl_PNCT2, &Declaration);
		break;
	case EVT_PNT2W:
		pID3DDevice->CreateVertexDeclaration(Decl_PNT2W_M, &Declaration);
		break;
	default:
		ASSERT(false);
	}
}

CD3D9VertexDeclaration::~CD3D9VertexDeclaration()
{
	SAFE_RELEASE(Declaration);
}

#endif